package corp.edu.amadeus.core.schedule;

import corp.edu.amadeus.core.schedule.task.InquiryTask;
import java.util.Timer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.Logger;
import corp.edu.amadeus.core.schedule.task.InquiryTask;

public class TaskService {

	private static Logger log = Logger.getLogger(TaskService.class);

	public void initTasks() {
		log.debug("++++ Inicializando tareas programadas......");
		int hour=12, minute = 0;
		Timer task = new Timer();
		Calendar hoy = Calendar.getInstance();
	    SimpleDateFormat sdF = new SimpleDateFormat("dd/MM/yyyy");
	    String[] dateComps = sdF.format(hoy.getTime()).split("/");
	    int day = Integer.parseInt(dateComps[0]);
	    int month = Integer.parseInt(dateComps[1]);
	    int year = Integer.parseInt(dateComps[2]);
	    Calendar prog = Calendar.getInstance();
	    prog.set(Calendar.YEAR, year);
	    prog.set(Calendar.MONTH, month-1);
	    prog.set(Calendar.DAY_OF_MONTH,day);
	    prog.set(Calendar.HOUR_OF_DAY, hour);
	    prog.set(Calendar.MINUTE, minute);
	    prog.set(Calendar.SECOND, 0);
	    double diferencia = (prog.getTimeInMillis() - hoy.getTimeInMillis()  )/1000F/60F/60F;
	    if(diferencia<0) {
		diferencia = 24+diferencia;
	    }
	    int delay = Integer.parseInt(""+Math.round(diferencia*60*60*1000));
	    Timer progPub = new Timer();
	    progPub.schedule(new InquiryTask(),
				delay,
				86400000);
	}
}
